/**
 * A layer encapsulate a bunch of containers and wires
 * @class WebUILayer
 * @namespace WireIt
 * @constructor
 * @param {Object}   options   Configuration object (see the properties)
 * @author Igor Lazarev
 */

WireIt.WebUILayer = function(options) {


WireIt.WebUILayer.superclass.constructor.call(this, options);


/**
* Event that is fired when the focus has moved to a container
* You can register this event with myTerminal.eventContainerFocused.subscribe(function(e,params) { var container=params[0];}, scope);
* @event eventContainerFocused
* Igor Lazarev
*/
this.eventContainerFocused = new YAHOO.util.CustomEvent("eventContainerFocused");

function mouseHandler(e) {
		
    var elTarget = YAHOO.util.Event.getTarget(e);    
		
	if (elTarget.id == "layer") { //empty area id == layer
		try {
						
			if (this.focusedContainer){
				this.focusedContainer.removeFocus();
				}
		}
		catch(ex) {
        //alert("Mouse handler exception: " + ex);
		this.log("Mouse handler exception: " + ex);
		if (console) {
			console.log(ex);
		}
		}
    } 
}
	
YAHOO.util.Event.on(document.getElementById("center"), "click", mouseHandler, this, true); 

}

YAHOO.extend(WireIt.WebUILayer, WireIt.Layer, {

//set focus to the new container - causes instability
//container.onMouseDown();

addContainer: function(containerConfig){
	
	var type = eval('('+(containerConfig.xtype || "WireIt.WebUIContainer")+')');
	  if(!YAHOO.lang.isFunction(type)) {
	     throw new Error("WireIt layer unable to add container: xtype '"+containerConfig.xtype+"' not found");
	  }

	containerConfig.position = YAHOO.lang.JSON.parse(YAHOO.lang.JSON.stringify(containerConfig.position).replace(/-/g,""));	 
	var container = new type(containerConfig, this);//terminals are created here
	  this.containers.push( container );

	  // Event listeners
	  container.eventAddWire.subscribe(this.onAddWire, this, true);
	  container.eventRemoveWire.subscribe(this.onRemoveWire, this, true);
	
	  if(container.ddResize) {
	     container.ddResize.on('endDragEvent', function() {
	        this.eventContainerResized.fire(container);
	     }, this, true);
	  }
	  
	  if(container.dd) {
	     container.dd.on('endDragEvent', function() {
	        this.eventContainerDragged.fire(container);
	     }, this, true);
	  }
	
	this.eventAddContainer.fire(container);
	//alert("value1: " + YAHOO.lang.JSON.stringify(container.getValue()));
	
	//a hack to set default values of boolean parameters to false
	if ((typeof(container.getValue().input)!="undefined")&&(container.getValue().input.type=="boolean")){	
		value=container.getValue();		
		value.input.inputParams.value=false;
		container.setValue(value);
	}

	return container;
		
},

render: function() {
	WireIt.WebUILayer.superclass.render.call(this);
		
	this.el.id = "layer";
},
   
focusContainer: function (container) {
	this.eventContainerFocused.fire(container);
},

getWiring: function(){
	
	  var i;
      var obj = WireIt.WebUILayer.superclass.getWiring.call(this);
  		
      obj.wires.clear();
	  
	  for( i = 0 ; i < this.wires.length ; i++) {
         var wire = this.wires[i];
      
         var wireObj = { 
            src: {moduleId: WireIt.indexOf(wire.terminal1.container, this.containers), terminalId: WireIt.indexOf(wire.terminal1, wire.terminal1.container.terminals)}, 
            tgt: {moduleId: WireIt.indexOf(wire.terminal2.container, this.containers), terminalId: WireIt.indexOf(wire.terminal2, wire.terminal2.container.terminals)} 
         };
         obj.wires.push(wireObj);
      }
   
      return obj;
	
}

	
});
